package com.sk.cardeal.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sk.cardeal.R;

/**
 * Created by sk on 2017/12/15.
 */

public class IconViewTextView extends LinearLayout {

    private ImageView icon;
    private TextView title;
    private int titleColor;

    public IconViewTextView(Context context) {
        this(context, null);
    }

    public IconViewTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.M)
    public IconViewTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.item_icon_with_text, this);
        title = findViewById(R.id.lm_title);
        icon = findViewById(R.id.lm_icon);
        ImageView more = findViewById(R.id.icon);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.IconViewTextView);
        int count = ta.getIndexCount();
        for (int i = 0; i < count; i++) {
            int itemId = ta.getIndex(i);
            switch (itemId) {
                case R.styleable.IconViewTextView_icon:
                    icon.setImageResource(ta.getResourceId(itemId, 0));
                    break;
                case R.styleable.IconViewTextView_iconText:
                    setTitle(ta.getString(itemId));
                    break;
                case R.styleable.IconViewTextView_titleColor:
                    setTitleColor(ta.getColor(itemId, R.color.hint_color));
                    break;
                case R.styleable.IconViewTextView_iconVisiable:
                    boolean canSee = ta.getBoolean(itemId, true);
                    if (canSee) {
                        more.setVisibility(VISIBLE);
                    } else {
                        more.setVisibility(INVISIBLE);
                    }
                    break;
            }
        }
    }

    private void setTitle(String s) {
        title.setText(s);
    }

    private void setTitleColor(int color) {
        title.setTextColor(color);
    }


}
